package com.xzz.number;

/**
 * @author: hhz
 * @create: 2022-01-13 13:04
 * 747. 至少是其他数字两倍的最大数
 **/
public class DominantIndex {
    public static void main(String[] args) {
        System.out.println(dominantIndex(new int[]{3,6,1,0}));
        System.out.println(dominantIndex(new int[]{1,2,3,4}));
        System.out.println(dominantIndex(new int[]{1}));
        System.out.println(dominantIndex(new int[]{1, 0}));
        System.out.println(dominantIndex(new int[]{1,5,2,55,3,44}));
    }

    public static int dominantIndex(int[] nums) {
        if (nums.length == 1) {
            return 0;
        }
        int maxIndex = 0;
        int sendMaxIndex = 1;
        if(nums[maxIndex]<nums[sendMaxIndex]){
            maxIndex = 1;
            sendMaxIndex = 0;
        }
        for (int i = 2; i < nums.length; i++) {
            if(nums[i]>=nums[maxIndex]){
                sendMaxIndex = maxIndex;
                maxIndex = i;
            }else {
                if(nums[i]>=nums[sendMaxIndex]){
                    sendMaxIndex = i;
                }
            }
        }
        return nums[maxIndex] >= (nums[sendMaxIndex] << 1) ? maxIndex : -1;
    }
}
